#include "weather.h"


Weather::Weather()
{
    save_cfg.begin("Config");
    key = save_cfg.getString("key", "");
    city_name = save_cfg.getString("cityname", "");

    scr = lv_obj_create(NULL, NULL);
    lv_obj_set_style_local_bg_color(scr, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
	lv_scr_load(scr);

    lv_style_init(&font_style_tem);
	lv_style_set_text_font(&font_style_tem, LV_STATE_DEFAULT, &lv_font_montserrat_48);
	lv_style_set_text_color(&font_style_tem, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    lv_style_init(&font_style_city);
	lv_style_set_text_font(&font_style_city, LV_STATE_DEFAULT, &lv_font_montserrat_34);
	lv_style_set_text_color(&font_style_city, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    label_city = lv_label_create(scr, NULL);
	lv_obj_add_style(label_city, LV_LABEL_PART_MAIN, &font_style_city);
    lv_label_set_text(label_city, "loading...");
    lv_obj_align(label_city, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_task_handler();

    lv_style_set_text_font(&font_style_city, LV_STATE_DEFAULT, &lv_font_montserrat_28);
    img = lv_img_create(scr, NULL);
    lv_obj_align(img, NULL, LV_ALIGN_CENTER, 0, 0);

    label_tem = lv_label_create(scr, NULL);
	lv_obj_add_style(label_tem, LV_LABEL_PART_MAIN, &font_style_tem);

    lv_obj_align(img, NULL, LV_ALIGN_CENTER, -50, -65);
    lv_obj_align(label_tem, NULL, LV_ALIGN_CENTER, 55, 80);
    lv_obj_align(label_city, NULL, LV_ALIGN_CENTER, -40, -95);
    lv_label_set_text(label_city, (city_name).c_str());
    timecal = millis()-300000;
    code = "0";
    temperature = "0";
    if (WiFi.status()!=WL_CONNECTED)
    {
        lv_img_set_src(img, ("S:/weather/" + code + ".bin").c_str());
        lv_label_set_text(label_tem, (temperature + "°C").c_str());
        lv_task_handler();
    }
}

Event Weather::main_process(Action action)
{
    event = NOEVENT;
    show_weather();
    return event;
}

void Weather::show_weather()
{
    if (millis()-timecal>300000)
    {
        event = RGB_FLIKER;
        if (WiFi.status()==WL_CONNECTED&&update_weather())
        {
            lv_img_set_src(img, ("S:/weather/" + code + ".bin").c_str());
            // lv_img_set_src(img, &weather_icon_list[code.toInt()]);
            lv_label_set_text(label_tem, (temperature + "°C").c_str());
            lv_task_handler();
            timecal = millis();
        }     
    }
}

bool Weather::update_weather()
{
	String Msg = "";
	HTTPClient http;
    bool is_update=false;
	http.begin(("https://api.seniverse.com/v3/weather/now.json?key=" + key + "&location=" + city_name + "&language=zh-Hans&unit=c").c_str());
	// start connection and send HTTP headerFFF
	int httpCode = http.GET();
	// httpCode will be negative on error
	if (httpCode > 0)
	{
		// file found at server
        
		if (httpCode == HTTP_CODE_OK)
		{
            Msg = http.getString();
            int pos = Msg.indexOf("\"code\":\"");
            code = Msg.substring(pos + 8, Msg.length());
            code = code.substring(0, code.indexOf("\""));
            pos = Msg.indexOf("\"temperature\":\"");
            temperature = Msg.substring(pos + 15, Msg.length());
            temperature = temperature.substring(0, temperature.indexOf("\""));
            is_update = true;
		}
        else{
            is_update = false;
        }
	}
	else
	{
        is_update = false;
	}
    http.end();
    return is_update;
}

Weather::~Weather()
{
    lv_obj_del(scr);
}